package com.example.mq.mqserver.datacenter;

import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.core.Message;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * 用这个来管理数据库和文件中的数据
 * 使用只需要使用这个类就可以操控数据库和文件（相当于是进一步封装，不用关注具体怎么实现，只看到方法就知道是干嘛的）
 */
public class DiskDataManage {
    //使用这个类来操作数据库中数据
    private DataBaseManage dataBaseManage = new DataBaseManage();
    //使用这个类来操作文件中的相关数据
    private MessageFileManage messageFileManage = new MessageFileManage();

    public void init() {
        dataBaseManage.init();
        //此处的init是空方法，等后续需要用的时候，直接在这初始化就行
        messageFileManage.init();
    }


    public void deleteDB() {
        dataBaseManage.deleteDB();
    }

    //封装交换机相关操作
    public void insertExchange(Exchange exchange) {
        dataBaseManage.insertExchange(exchange);
    }
    public void deleteExchange(String exchangeName) {
        dataBaseManage.deleteExchange(exchangeName);
    }
    public List<Exchange> selectExchanges() {
        return dataBaseManage.selectExchanges();
    }


    //封装队列相关操作
    public void insertQueue(MSGQueue queue) {
        dataBaseManage.insertQueue(queue);
    }
    public void deleteQueue(String queueName) {
        dataBaseManage.deleteQueue(queueName);
    }
    public List<MSGQueue> selectQueues() {
        return dataBaseManage.selectQueues();
    }

    //封装Binding相关操作
    public void insertBinding(Binding binding) {
        dataBaseManage.insertBinding(binding);
    }
    public void deleteBinding(Binding binding) {
        dataBaseManage.deleteBinding(binding);
    }
    public List<Binding> selectBindings() {
        return dataBaseManage.selectBindings();
    }

    //封装文件相关操作
    public void createQueueFiles(String queueName) throws IOException {
        messageFileManage.createQueueFiles(queueName);
    }
    public void deleteQueueFiles(String queueName) throws IOException {
        messageFileManage.deleteQueue(queueName);
    }
    public void writeMessage(MSGQueue queue, Message message) throws IOException {
        messageFileManage.writeMessage(queue,message);
    }
    public void deleteMessage(MSGQueue queue, Message message) throws IOException, ClassNotFoundException {
        messageFileManage.deleteMessage(queue,message);
    }
    public LinkedList<Message> loadMessages(String queueName) throws IOException, ClassNotFoundException {
        return messageFileManage.loadAllMessagesFromQueue(queueName);
    }
    public void gc(MSGQueue queue) throws IOException, ClassNotFoundException {
        messageFileManage.gc(queue);
    }

}
